package com.whj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whj.entity.Cart;
import com.whj.entity.Goods;
import com.whj.mapper.CartDao;
import com.whj.mapper.GoodsDao;
import com.whj.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartDao cartDao;
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public List<Cart> showCart() {
        return cartDao.selectList(null);
    }

    @Override
    public void addCart(Integer id, Integer userId) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Goods goods = goodsDao.selectOne(queryWrapper);
        System.out.println(goods);
        Cart cart = new Cart();
        cart.setId(null);
        cart.setGoodsId(id);
        cart.setUserId(userId);
        cart.setStatus(0);
        cart.setNum(1);
        cart.setGoodsName(goods.getName());
        cart.setGoodsImg(goods.getImg());
        cart.setGoodsPrice(goods.getPrice());
        cartDao.insert(cart);
    }

    @Override
    public Cart selectOne(Integer id) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", id);
        return cartDao.selectOne(queryWrapper);
    }

    @Override
    public void update(Cart cart) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("num", cart.getNum()+1);
        cartDao.update(cart, queryWrapper);
    }
    @Override
    public List<Cart> selectListById(Integer id) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        return cartDao.selectList(queryWrapper);
    }

    @Override
    public void deleteCart(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        cartDao.deleteByMap(map);
    }

    @Override
    public void deleteCartByid(Integer id) {
        cartDao.deleteById(id);
    }
}
